﻿using System.Linq;

namespace CometDocs
{
    /// <summary>
    /// A structure that wraps the identification information of a file.
    /// </summary>
    public class FileInfo
    {
        /// <summary>
        /// The file CometDocs ID.
        /// </summary>
        public long ID { get; set; }

        /// <summary>
        /// The file name without extension: e.g. for "Test.pdf" would be "Test".
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The file extension: e.g. for "Test.pdf" would be "pdf".
        /// </summary>
        public string Extension { get; set; }

        /// <summary>
        /// The file size in bytes.
        /// </summary>
        public long Size { get; set; }

        /// <summary>
        /// True if the file has some conversions, e.g. if "Test.pdf" has already been converted to "Test.xls".
        /// </summary>
        public bool HasConversions { get; set; }

        /// <summary>
        /// Create a new FileInfo instance.
        /// If the provided name is "Test.pdf" the resulting File instance "Name" will be "Test" and the "Extension" "pdf".
        /// </summary>
        /// <param name="nameWithExtension">The full file name: e.g. "Test.pdf".</param>
        public FileInfo(string nameWithExtension = null)
        {
            if (nameWithExtension != null)
            {
                string[] tokens = nameWithExtension.Split('.');

                if (tokens.Length >= 2)
                {
                    Name = tokens[0];
                    for (int i = 1; i < tokens.Length - 1; ++i)
                    {
                        Name += "." + tokens[i];
                    }

                    Extension = tokens.Last();
                }
                else
                {
                    Name = tokens.Single();
                }
            }
        }

        /// <summary>
        /// Get the object hashcode: only based on the ID.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

        /// <summary>
        /// Compare two FileObject: only based on the IDs.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            FileInfo other = obj as FileInfo;

            return other != null && other.ID == this.ID;
        }
    }
}
